package com.jrx.anytxn.param.controller.investor;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.investor.PrInvestorPenaltyRateReq;
import com.jrx.anytxn.param.dto.investor.PrInvestorPenaltyRateRes;
import com.jrx.anytxn.param.service.investor.IPrInvestorPenaltyRateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * 出资方罚息利率表
 *
 * @author 黄秋平
 * @date 2020年5月18日17:25:45
 */
@Api(tags = "出资方罚息利率表参数API接口")
@RestController
@RequestMapping(value = "/param/investor/penaltyRates")
public class PrInvestorPenaltyRateController {
    private static final Logger logger = LoggerFactory.getLogger(PrInvestorPenaltyRateController.class);

    @Resource
    IPrInvestorPenaltyRateService iPrInvestorPenaltyRateService;

    @Resource
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID和罚息利率表ID查询出资方罚息利率表信息", notes = "根据租户ID和罚息利率表ID查询出资方罚息利率表信息")
    @GetMapping(value = "/{tenantId}/{investorPenaltyId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "investorPenaltyId", value = "出资方罚息利率表ID", required = true)
    })
    public TxnRespResult<PrInvestorPenaltyRateRes> getByTenIdAndPenaltyId(@NotNull @PathVariable String tenantId, @PathVariable String investorPenaltyId) {
        TxnRespResult<PrInvestorPenaltyRateRes> result = new TxnRespResult<>();
        PrInvestorPenaltyRateRes res;
        try {
            res = iPrInvestorPenaltyRateService.findByTenIdAndPenaltyId(String.valueOf(infoService.getUser().getProjectId()), investorPenaltyId);
            if (res.getIncreaseNum() != null && res.getIncreaseNum().compareTo(BigDecimal.valueOf(0)) != 0) {
                res.setIncreaseNum(res.getIncreaseNum().multiply(BigDecimal.valueOf(100)));
            }
            if (res.getTaxRate() != null && res.getTaxRate().compareTo(BigDecimal.valueOf(0)) != 0) {
                res.setTaxRate(res.getTaxRate().multiply(BigDecimal.valueOf(100)));
            }
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户Id分页查询出资方罚息利率表信息", notes = "根据租户Id分页查询出资方罚息利率表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrInvestorPenaltyRateRes>> getAllPageByTenId(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {

        TxnRespResult<TxnPage<PrInvestorPenaltyRateRes>> result = new TxnRespResult<>();
        TxnPage<PrInvestorPenaltyRateRes> response;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            response = iPrInvestorPenaltyRateService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增出资方罚息利率表信息", notes = "新增出资方罚息利率表信息")
    @PostMapping("/")
    public TxnRespResult<PrInvestorPenaltyRateRes> createInvestorPenaltyRate(@RequestBody PrInvestorPenaltyRateReq prPenaltyRateTableReq) {
        PrInvestorPenaltyRateRes res;
        TxnRespResult<PrInvestorPenaltyRateRes> result = new TxnRespResult<>();
        try {
            prPenaltyRateTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prPenaltyRateTableReq.setUpdateBy(infoService.getUser().getUserName());
            if (prPenaltyRateTableReq.getIncreaseNum() != null && prPenaltyRateTableReq.getIncreaseNum().compareTo(BigDecimal.ZERO) != 0) {
                prPenaltyRateTableReq.setIncreaseNum(prPenaltyRateTableReq.getIncreaseNum().divide(BigDecimal.valueOf(100)));
            }
            if (prPenaltyRateTableReq.getTaxRate() != null && prPenaltyRateTableReq.getTaxRate().compareTo(BigDecimal.ZERO) != 0) {
                prPenaltyRateTableReq.setTaxRate(prPenaltyRateTableReq.getTaxRate().divide(BigDecimal.valueOf(100)));
            }
            res = iPrInvestorPenaltyRateService.addPenaltyRateTable(prPenaltyRateTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "编辑出资方罚息利率表信息", notes = "编辑出资方罚息利率表信息")
    @PutMapping("/")
    public TxnRespResult<PrInvestorPenaltyRateRes> modifyInvestorPenaltyRate(@RequestBody PrInvestorPenaltyRateReq prPenaltyRateTableReq) {
        TxnRespResult<PrInvestorPenaltyRateRes> result = new TxnRespResult<>();
        PrInvestorPenaltyRateRes res;
        try {
            if (prPenaltyRateTableReq.getIncreaseNum() != null && prPenaltyRateTableReq.getIncreaseNum().compareTo(BigDecimal.ZERO) != 0) {
                prPenaltyRateTableReq.setIncreaseNum(prPenaltyRateTableReq.getIncreaseNum().divide(BigDecimal.valueOf(100)));
            }
            if (prPenaltyRateTableReq.getTaxRate() != null && prPenaltyRateTableReq.getTaxRate().compareTo(BigDecimal.ZERO) != 0) {
                prPenaltyRateTableReq.setTaxRate(prPenaltyRateTableReq.getTaxRate().divide(BigDecimal.valueOf(100)));
            }
            prPenaltyRateTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = iPrInvestorPenaltyRateService.modifyPenaltyRateTable(prPenaltyRateTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和罚息利率表ID删除出资方罚息利率表信息", notes = "根据租户ID和罚息利率表ID删除出资方罚息利率表信息")
    @DeleteMapping(value = "/{tenantId}/{investorPenaltyId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户Id", required = true),
            @ApiImplicitParam(name = "investorPenaltyId", value = "出资方罚息利率表ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndPenaltyId(@NotNull @PathVariable String tenantId, @PathVariable String investorPenaltyId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            flag = iPrInvestorPenaltyRateService.removeByTenIdAndPenaltyId(String.valueOf(infoService.getUser().getProjectId()), investorPenaltyId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

}
